package org.x.usermod.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.x.commonmod.config.Result;
import org.x.usermod.entity.SysConfigEntity;
import org.x.usermod.entity.SysConfigHistoryEntity;
import org.x.usermod.service.ConfigService;

import java.util.List;

@RestController
@RequestMapping("/config")
public class ConfigController {

    @Autowired
    private ConfigService configService;

    /**
     * 获取所有配置
      * @return
     */
    @GetMapping("/all")
    public Result getAllConfig(){
        List<SysConfigEntity> result =configService.getConfigList();
        return Result.ok("查询成功",result);
    }

    /**
     * 刷新缓存
     * @return
     */
    @PostMapping("/refreshCache")
    public Result refreshCache(){
        List<SysConfigEntity> result=configService.refreshConfig();
        return Result.ok("刷新成功",result);
    }

    /**
     * 查询公共配置项
     * @return
     */
    @GetMapping("/public")
    public Result getPublicConfig(){
        List<SysConfigEntity> result=configService.getPublicConfig();
        return Result.ok("查询成功",result);
    }

    /**
     * 更新配置
     * @return
     */
    @PutMapping("/update")
    public Result updateConfig(@RequestParam("id")Integer id,
                               @RequestParam("configValue")String configValue){
        configService.updateConfig(id,configValue);
        return Result.ok("更新成功");
    }

    /**
     * 根据条件查询配置历史，仅提供根据配置名称和配置人进行查询
     * @param configKey 配置名称
     * @param operatorId 配置人
     * @return
     */
    @GetMapping("/history")
    public Result getHistoryList(@RequestParam("configKey")String configKey,
                                 @RequestParam("operatorId")String operatorId,
                                 @RequestParam("page")Integer page,
                                 @RequestParam("pageSize")Integer pageSize){
        List<SysConfigHistoryEntity> result=configService.getHistoryList(configKey,operatorId,page,pageSize);
        return Result.ok("查询成功",result);
    }

    /**
     * 根据id查询配置详情
     * @param id 配置id
     * @return
     */
    @GetMapping("/detail")
    public Result getConfigDetail(@RequestParam("id")Integer id){
        SysConfigEntity result=configService.getConfigDetail(id);
        return Result.ok("查询成功",result);
    }
    /**
     * 根据配置键查询配置详情
     * @param configKey 配置键
     * @return
     */
    @GetMapping("/search")
    public Result getConfigByKey(@RequestParam("configKey")String configKey){
        List<SysConfigEntity> result=configService.getConfigByKey(configKey);
        return Result.ok("查询成功",result);
    }
}
